package com.chen.blog.common.component;

import com.chen.blog.common.api.CommonResult;
import com.chen.blog.common.constant.MessageConstant;
import com.chen.blog.model.Admin;
import com.chen.blog.model.Blog;
import com.chen.blog.model.User;
import com.chen.blog.service.AdminService;
import com.chen.blog.service.BlogService;
import com.chen.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Description 通用更新图片操作
 * @Author zgyh2
 * @Date 2022/1/29
 * @Version 1.0
 */
@Component
public class ImgUtil {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    public CommonResult updatePic(MultipartFile multipartFile, Integer id, Object o) {
        if (multipartFile.isEmpty()) {
            return CommonResult.error(MessageConstant.UPDATE_POSITION_FAIL);
        }
        String fileName = System.currentTimeMillis() + multipartFile.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "img";
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdir();
        }
        //上传后的文件地址
        File realPath = new File(filePath + System.getProperty("file.separator") + fileName);
        try {
            multipartFile.transferTo(realPath);
            Admin admin = adminService.getById(id);
            User user = userService.getById(id);
            Blog blog = blogService.getById(id);
            if (o.equals(admin)) {
                File picFile = new File(System.getProperty("user.dir") + admin.getImg());
                if (picFile.exists()) {
                    picFile.delete();
                }
                admin.setImg("/img/" + fileName); //更新数据库文件的相对地址
                if (adminService.updateById(admin)) {
                    return CommonResult.success(MessageConstant.UPDATE_POSITION_SUCCESS, admin.getImg());
                }
                return CommonResult.error(MessageConstant.UPDATE_POSITION_FAIL);
            } else if (o.equals(user)) {
                File picFile = new File(System.getProperty("user.dir") + user.getImg());
                if (picFile.exists()) {
                    picFile.delete();
                }
                user.setImg("/img/" + fileName); //更新数据库文件的相对地址
                if (userService.updateById(user)) {
                    return CommonResult.success(MessageConstant.UPDATE_POSITION_SUCCESS, user.getImg());
                }
                return CommonResult.error(MessageConstant.UPDATE_POSITION_FAIL);
            } else if (o.equals(blog)) {
                File picFile = new File(System.getProperty("user.dir") + blog.getImg());
                if (picFile.exists()) {
                    picFile.delete();
                }
                blog.setImg("/img/" + fileName); //更新数据库文件的相对地址
                if (blogService.updateById(blog)) {
                    return CommonResult.success(MessageConstant.UPDATE_POSITION_SUCCESS, blog.getImg());
                }
                return CommonResult.error(MessageConstant.UPDATE_POSITION_FAIL);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return CommonResult.error(MessageConstant.MYSQL_DATA_FAIL);
        }
        return CommonResult.error(MessageConstant.UPDATE_POSITION_FAIL);
    }
}
